package com.sanji.sjmallmodule.account;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallutils.Tools;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/***
 * 完善信息
 */
public class ImproveActivity extends BaseActivity implements View.OnClickListener, NoHttpListener {

    @BindView(R.id.common_back_img)
    ImageView commonBackImg;
    @BindView(R.id.common_left_tv)
    TextView commonLeftTv;
    @BindView(R.id.channel_code_edt)
    EditText channelCodeEdt;
    @BindView(R.id.clear_channel_coding_img)
    ImageView clearChannelCodingImg;
    @BindView(R.id.bank_account_edt)
    EditText bankAccountEdt;
    @BindView(R.id.bank_card_number_edt)
    EditText bankCardNumberEdt;
    @BindView(R.id.clear_bank_card_number_img)
    ImageView clearbankCardNumberImg;
    @BindView(R.id.bank_card_name_edt)
    EditText bankCardNameEdt;
    @BindView(R.id.clear_bank_card_name_img)
    ImageView clearBankCardNameImg;
    @BindView(R.id.confirm_save_btn)
    Button confirmSaveBtn;
    private Request<JSONObject> request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_improve);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        commonBackImg.setOnClickListener(this);
        commonLeftTv.setText(getString(R.string.perfect_info));
        clearChannelCodingImg.setOnClickListener(this);
        clearbankCardNumberImg.setOnClickListener(this);
        clearBankCardNameImg.setOnClickListener(this);
        confirmSaveBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
            case R.id.clear_bank_card_name_img:
                bankCardNameEdt.setText("");
                break;
            case R.id.clear_channel_coding_img:
                channelCodeEdt.setText("");
                break;
            case R.id.clear_bank_card_number_img:
                bankCardNumberEdt.setText("");
                break;
            case R.id.confirm_save_btn:
                //点击了确认保存
                requestInfo();
                break;
        }
    }

    @Override
    public void onSuccessed(int what, Response response) {
        int requestCode = response.getHeaders().getResponseCode();
        JSONObject jsonObject = new JSONObject();
        if (what == ConfigAPI.IMPROVE_USER_INFO_URL.hashCode()) {
            jsonObject = (JSONObject) response.get();
            String status = jsonObject.optString(ConfigConts.STATUS_STRING);
            if (status.equals(ConfigConts.STATUS_SUCCESS)) {
                String successMsg = jsonObject.optString(ConfigConts.SUCCESSMSG);
                Tools.toastTools(mCtx, successMsg);
            } else {
                String errorMsg = jsonObject.optString(ConfigConts.ERRORMSG);
                Tools.toastTools(mCtx, errorMsg);
            }
        }
    }

    @Override
    public void onFailed(int what, Response response) {
        if (what == ConfigAPI.IMPROVE_USER_INFO_URL.hashCode()) {
            Tools.toastTools(mCtx, getString(R.string.false_message));
        }
    }

    /**
     * 完善信息
     */
    public void requestInfo() {
        request = NoHttp.createJsonObjectRequest(ConfigAPI.IMPROVE_USER_INFO_URL, RequestMethod.POST);
        JSONObject json = new JSONObject();
        String chanelCode = channelCodeEdt.getText().toString().trim();
        String bankAccount = bankAccountEdt.getText().toString().trim();
        String bankCard = bankCardNumberEdt.getText().toString().trim();
        String bankCardName = bankCardNameEdt.getText().toString().trim();
        String account = myShare.getString(ConfigConts.ACCOUNT, null);
        String idCard = myShare.getString(ConfigConts.ID_CARD, null);
        try {
            json.put("account", account);
            json.put("channelCoding", chanelCode);
            json.put("bankCard", bankCard);
            json.put("depositBank", bankAccount);
            json.put("bankCardName", bankCardName);
            json.put("idCard", idCard);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.setDefineRequestBodyForJson(json);
        request.setCancelSign(ConfigAPI.IMPROVE_USER_INFO_URL.hashCode());
        NoHttpCallBack.getInstance().add(mCtx, ConfigAPI.IMPROVE_USER_INFO_URL.hashCode(), request, this, true, true);
    }
}
